package com.brozers.islamdini;







import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class Settings extends PreferenceActivity implements OnPreferenceChangeListener, OnSharedPreferenceChangeListener{
	 static String cityNamer;
	 Boolean first;
	 String LangaugeID[];
	 String city_dic[];
	 String lang_dic[];
	 String [] cityID;
	 String [] cityName;
	 String[] numMonth;
	 String [] cityName_kz;
	 String[] numMonth_kz;
	 String [] cityName_ru;
	 String[] numMonth_ru;
	 String [] cityName_en;
	 String[] numMonth_en;
	 int city_lang[] =  { R.array.cityName , R.array.cityName_ru ,R.array.cityName_en};
	 int id;
	 int order;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 setPreferenceScreen(createPreferenceHierarchy());
	}
	 private PreferenceScreen createPreferenceHierarchy() {
	        // Root
	        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
	        PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
	        root.addPreference(dialogBasedPrefCat);
	        
	        LangaugeID = getResources().getStringArray(R.array.languageID);
	        city_dic = getResources().getStringArray(R.array.city_dic);
	        lang_dic = getResources().getStringArray(R.array.lang_dic);
	        
	        cityName_kz = getResources().getStringArray(R.array.cityName);
			numMonth_kz = getResources().getStringArray(R.array.month);
			
			cityName_ru = getResources().getStringArray(R.array.cityName_ru);
			numMonth_ru = getResources().getStringArray(R.array.month_ru);
			
			cityName_en = getResources().getStringArray(R.array.cityName_en);
			numMonth_en = getResources().getStringArray(R.array.month_en);
			
			
			
	        id = MainActivity.settings.getInt("Language", 0)-1;
	        order = MainActivity.settings.getInt("order", 0);
			setLanguage();
			
	        ListPreference listPref1 = new ListPreference(this);
	        listPref1.setEntries(R.array.languageID);
	        listPref1.setEntryValues(R.array.languageID);
	        listPref1.setDialogTitle(lang_dic[id]);
	        listPref1.setKey("language_lng");
	        listPref1.setSummary( LangaugeID[id]);
	        listPref1.setTitle(lang_dic[id]);
	        listPref1.setValue(LangaugeID[id]);
	        dialogBasedPrefCat.addPreference(listPref1);
	        listPref1.setOnPreferenceChangeListener(this);
	        
	        ListPreference listPref = new ListPreference(this);
	        listPref.setEntries(city_lang[id]);
	        listPref.setEntryValues(city_lang[id]);
	        listPref.setDialogTitle(city_dic[id]);
	        listPref.setKey("list_preference");
	        listPref.setSummary(cityName[order]);
	        listPref.setTitle(city_dic[id]);
	        listPref.setValue(cityName[order]);
	        dialogBasedPrefCat.addPreference(listPref);
	        listPref.setOnPreferenceChangeListener(this);
	        
	       	        return root;
	 }
	 public void setLanguage(){
			switch (id) {
			case 0:
				cityName = cityName_kz;
				numMonth = numMonth_kz;
				
				break;
			case 1:
				cityName =cityName_ru;
				numMonth = numMonth_ru;
				
				break;
			case 2:
				cityName =cityName_en;
				numMonth = numMonth_en;
				
				break;

			default:
				break;
			}
		}
	 @Override
	    protected void onResume() {
	        super.onResume();
	        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	    }

	    @Override
	    protected void onPause() {
	        super.onPause();
	        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	    }

	    @Override
	    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
	        
	    }
	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		
		if(preference.getKey().equals("language_lng")){
			
			findPreference("language_lng").setSummary(newValue.toString());
			
			if(newValue.toString().equals("Қазақ тілі")){
				findPreference("language_lng").setTitle("Тіл");
				findPreference("list_preference").setTitle("Қала");
				((ListPreference) findPreference("list_preference")).setEntries(city_lang[0]);
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putInt("Language", 1);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
			    
			}
			else if(newValue.toString().equals("Русский язык")){
				findPreference("language_lng").setTitle("Язык");
				findPreference("list_preference").setTitle("Город");
				((ListPreference) findPreference("list_preference")).setEntryValues(city_lang[1]);
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putInt("Language", 2);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				
			}
			else if(newValue.toString().equals("English Language")){
				findPreference("language_lng").setTitle("Language");
				findPreference("list_preference").setTitle("City");
				((ListPreference) findPreference("list_preference")).setEntryValues(city_lang[2]);
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putInt("Language", 3);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
			    
			}
			
		}
		
		else if(preference.getKey().equals("list_preference")){
			findPreference("list_preference").setSummary(newValue.toString());
		if(newValue.toString().equals("Алматы") || newValue.toString().equals("Almaty")){
			MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
			MainActivity.editor = MainActivity.settings.edit();
		    MainActivity.editor.putString("cityID", "8408");
		    MainActivity.editor.putInt("order",0);
			MainActivity.editor.commit();
			 setPreferenceScreen(createPreferenceHierarchy());
			}
			
			else if(newValue.toString().equals("Астана")|| newValue.toString().equals("Astana")){
			MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
			MainActivity.editor = MainActivity.settings.edit();
		    MainActivity.editor.putString("cityID", "8359");
		    MainActivity.editor.putInt("order",1);
		    MainActivity.editor.commit();
		    setPreferenceScreen(createPreferenceHierarchy());
			}
			else if(newValue.toString().equals("Ақтау")|| newValue.toString().equals("Актау")|| newValue.toString().equals("Aqtau")){
			MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
			MainActivity.editor = MainActivity.settings.edit();
		    MainActivity.editor.putString("cityID", "8376");
		    MainActivity.editor.putInt("order",2);
		    MainActivity.editor.commit();
		    setPreferenceScreen(createPreferenceHierarchy());
					
			
			}
			else if(newValue.toString().equals("Ақтөбе")|| newValue.toString().equals("Актобе")|| newValue.toString().equals("Aqtobe")){
			MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
			MainActivity.editor = MainActivity.settings.edit();
		    MainActivity.editor.putString("cityID", "8382");
		    MainActivity.editor.putInt("order",3);
		    MainActivity.editor.commit();
		    setPreferenceScreen(createPreferenceHierarchy());
			}
			else if(newValue.toString().equals("Арқалық")|| newValue.toString().equals("Аркалык")|| newValue.toString().equals("Arkalyk")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8496");
			    MainActivity.editor.putInt("order",4);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Атырау")|| newValue.toString().equals("Atyrau")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8427");
			    MainActivity.editor.putInt("order",5);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Атбасар")|| newValue.toString().equals("Atbasar")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8360");
			    MainActivity.editor.putInt("order",6);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Балқаш")|| newValue.toString().equals("Балхаш")|| newValue.toString().equals("Balkhash")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8462");
			    MainActivity.editor.putInt("order",7);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Байқоныр")|| newValue.toString().equals("Байконур")|| newValue.toString().equals("Baikonyr")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8455");
			    MainActivity.editor.putInt("order",8);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Екібастұз")|| newValue.toString().equals("Екибастуз")|| newValue.toString().equals("Ekibastuz")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "43827");
			    MainActivity.editor.putInt("order",9);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Есіл")|| newValue.toString().equals("Есил")|| newValue.toString().equals("Esil")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "43876");
			    MainActivity.editor.putInt("order",10);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Жаңаөзен")|| newValue.toString().equals("Жанаозен")|| newValue.toString().equals("Zhanaozen")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8370");
			    MainActivity.editor.putInt("order",11);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Жаңатас")|| newValue.toString().equals("Жанатас")|| newValue.toString().equals("Zhanatas")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8454");
			    MainActivity.editor.putInt("order",12);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Жезқазған")|| newValue.toString().equals("Жезказган")|| newValue.toString().equals("Zhezkazgan")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8463");
			    MainActivity.editor.putInt("order",13);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Көкшетау")|| newValue.toString().equals("Кокшетау")|| newValue.toString().equals("Kokshetau")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8531");
			    MainActivity.editor.putInt("order",14);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Қарағанды")|| newValue.toString().equals("Караганда")|| newValue.toString().equals("Karaganda")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8464");
			    MainActivity.editor.putInt("order",15);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Қостанай")|| newValue.toString().equals("Костанай")|| newValue.toString().equals("Kostanai")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8500");
			    MainActivity.editor.putInt("order",16);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Қызылорда")|| newValue.toString().equals("Кызылорда")|| newValue.toString().equals("Kyzylorda")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8518");
			    MainActivity.editor.putInt("order",17); 
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Құлсары")|| newValue.toString().equals("Кульсары")|| newValue.toString().equals("Kulsary")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8434");
			    MainActivity.editor.putInt("order",18);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Маңғышлақ")|| newValue.toString().equals("Мангышлак")|| newValue.toString().equals("Mangyshlak")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8369");
			    MainActivity.editor.putInt("order",19);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Мерке")|| newValue.toString().equals("Мерке")|| newValue.toString().equals("Merke")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "60034");
			    MainActivity.editor.putInt("order",20);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Орал")|| newValue.toString().equals("Уральск")|| newValue.toString().equals("Oral")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8560");
			    MainActivity.editor.putInt("order",21);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Өскемен")|| newValue.toString().equals("Усть-Каменогорск")|| newValue.toString().equals("Ust-Kamenogorsk")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8589");
			    MainActivity.editor.putInt("order",22);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
		
			else if(newValue.toString().equals("Павлодар")|| newValue.toString().equals("Pavlodar")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8525");
			    MainActivity.editor.putInt("order",23);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Петропавл")|| newValue.toString().equals("Petropavl")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8536");
			    MainActivity.editor.putInt("order",24);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Семей")|| newValue.toString().equals("Semei")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8583");
			    MainActivity.editor.putInt("order",25);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Талдықорған")|| newValue.toString().equals("Талдыкорган")|| newValue.toString().equals("Taldykorgan")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8423");
			    MainActivity.editor.putInt("order",26);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Талғар")|| newValue.toString().equals("Талгар")|| newValue.toString().equals("Talgar")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "62459");
			    MainActivity.editor.putInt("order",27);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Тараз")|| newValue.toString().equals("Taraz")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8447");
			    MainActivity.editor.putInt("order",28);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Түркістан")|| newValue.toString().equals("Туркестан")|| newValue.toString().equals("Turkestan")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8608");
			    MainActivity.editor.putInt("order",29);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
			else if(newValue.toString().equals("Шымкент")|| newValue.toString().equals("Чимкент")|| newValue.toString().equals("Shymkent")){
				MainActivity.settings =  PreferenceManager.getDefaultSharedPreferences(this);
				MainActivity.editor = MainActivity.settings.edit();
			    MainActivity.editor.putString("cityID", "8600");
			    MainActivity.editor.putInt("order",30);
			    MainActivity.editor.commit();
			    setPreferenceScreen(createPreferenceHierarchy());
				}
		}
		
		
		
		return false;
		
		
		
	}
	
	

	}
